package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;
   @Autowired
   private PurchaseListGoodsDao purchaseListGoodsDao;
    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }
   // 获取 商品库存信息列表进行分页展示
    @Override
    public Map listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String,Object> map = new HashMap<>();
        Long   total = goodsDao.getTotelRows(codeOrName,goodsTypeId);
        List<Goods> records = goodsDao.listInventory((page-1)*rows,rows,codeOrName,goodsTypeId);
        records.stream().forEach(this::getGoodsTypeInfo);
        map.put("total",total);
        map.put("rows",records);
        return map;
    }
  // 获取商品分页信息 进行展示
    @Override
    public Map<String, Object> goodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.goodsList((page-1)*rows,rows,goodsName,goodsTypeId);
        map.put("total",goodsDao.getTotelRows(goodsName, goodsTypeId));
        map.put("rows",goodsList);
        return map;
    }
  // 修改 或者保存 商品信息
    @Override
    public void saveOrUpdate(Long goodsId, Goods goods) {
         goods.setState(new Integer(0));
         if(ObjectUtils.isEmpty(goodsId)){
             goodsDao.save(goods);
         } else{
             goodsDao.update(goodsId,goods);
         }
    }

    @Override
    public String deleteGood(Integer goodsId) {
        // 先要进行查询
        //1 查询 商品状态
        Goods goods = goodsDao.getGoodsById(goodsId);
        if(goods.getState().compareTo(2)==0) return "商品入库，或者 有交易清单和销售单据";
        goodsDao.deleteGoods(goodsId); return "请求成功";
    }
  // 获取 无存储的 商品
    @Override
    public Map<String, Object> getNoInventoryQuantityGoodsList(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        Long total = goodsDao.getTotal(nameOrCode);
        List<Goods> goodsList =    goodsDao.getNoInventoryQuantityGoodsList((page-1)*rows,rows,nameOrCode);
        goodsList.stream().forEach(this::getGoodsTypeInfo);
       map.put("rows",goodsList);
       map.put("total",total);
       return map;
    }
// 获取 有存储的 商品
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        Long total = goodsDao.getTotalHasInventoryQuantity(nameOrCode);
        List<Goods> goodsList =   goodsDao.getHasInventoryQuantity((page-1)*rows,rows,nameOrCode);
        goodsList.stream().forEach(this::getGoodsTypeInfo);
        map.put("rows",goodsList);
        map.put("total",total);
        return map;
    }
// 修改库存和 销售价
    @Override
    public void updateInventoryQuantityOrPurchase(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
          goodsDao.updateInventoryQuantityOrPurchase(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public String deleteGoodStock(Integer goodsId) {
        Goods goods = goodsDao.getGoodsById(goodsId);
        if(goods!=null &&  goods.getState().compareTo(0)!=0){
            return  "不能进行修改，操作失败";
        }
        goodsDao.deleteGoodStock(goodsId);
        return "清库存成功";
    }
 // 获取 到库存小于最小库存的 商品
    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> list = goodsDao.listAlarm();
        map.put("rows",list);
        return map;
    }

    // 对商品信息进行一个补充
    private void getGoodsTypeInfo(Goods good) {
        good.setGoodsTypeName(goodsTypeDao.getGoodTypeName(good.getGoodsTypeId()));
        good.setSaleTotal(purchaseListGoodsDao.getSaleTotal(good.getGoodsId()));
    }


}
